package api

import (
    "encoding/json"
    "errors"
)

const(
    photoSpecUrl           = "https://apicall.id-photo-verify.com/"
    envPicAppKey           = "536abc6520c0ef6ca38c99e713d605c23d298987"
    cutPicAppKey           = "dde04cbee1894af9e5c2cd9b06163c72cfbebaf5"
    cutCheckPicAppKey      = "59c57ce7d98c3b825edc1f7d96dd49caf0de796c"
    cutChangeClothesAppKey = "6bdfbf0564e4cb89657fc264df5ce91b68e3c3ce"
    takeCutPicAppKey       = ""
    takeCutPicV2AppKey     = ""
)

type photoSpecApi struct{}

var PhotoSpecApi = new(photoSpecApi)

// EnvPic 制作证件照
func (r *photoSpecApi) EnvPic(req PhotoSpecEnvPicRequest) (*PhotoSpecEnvPicResponse, error) {
    // 参数格式化
    var err error
    params := make(map[string]interface{})
    jsonData, err := json.Marshal(req)
    if err != nil {
        return nil, err
    }
    err = json.Unmarshal(jsonData, &params)
    if err != nil {
        return nil, err
    }
    params["app_key"] = envPicAppKey

    var resp = new(PhotoSpecEnvPicResponse)
    uri := photoSpecUrl + "api/env_pic"
    dto := &RequestForm{
        Uri:    uri,
        Resp:   resp,
        Params: params,
    }
    err = Client.Post(dto)
    if err != nil {
        return nil, err
    }

    return resp, nil
}

// CutPic 制作证件照
func (r *photoSpecApi) CutPic(req PhotoSpecCutPicRequest) (*PhotoSpecCutPicResponse, error) {
    // 参数格式化
    var err error
    params := make(map[string]interface{})
    jsonData, err := json.Marshal(req)
    if err != nil {
        return nil, err
    }
    err = json.Unmarshal(jsonData, &params)
    if err != nil {
        return nil, err
    }
    params["app_key"] = cutPicAppKey

    var resp = new(PhotoSpecCutPicResponse)
    uri := photoSpecUrl + "api/cut_pic"
    dto := &RequestForm{
        Uri:    uri,
        Resp:   resp,
        Params: params,
    }
    err = Client.Post(dto)
    if err != nil {
        return nil, err
    }

    return resp, nil
}

// CutCheckPic 制作并检测证件照
func (r *photoSpecApi) CutCheckPic(req PhotoSpecCutCheckPicRequest) (*PhotoSpecCutCheckPicResponse, error) {
    // 参数格式化
    var err error
    params := make(map[string]interface{})
    jsonData, err := json.Marshal(req)
    if err != nil {
        return nil, err
    }
    err = json.Unmarshal(jsonData, &params)
    if err != nil {
        return nil, err
    }
    params["app_key"] = cutCheckPicAppKey

    var resp = new(PhotoSpecCutCheckPicResponse)
    uri := photoSpecUrl + "api/cut_check_pic"
    dto := &RequestForm{
        Uri:    uri,
        Resp:   resp,
        Params: params,
    }
    err = Client.Post(dto)
    if err != nil {
        return nil, err
    }
    if resp.Error != "" {
        return nil, errors.New(resp.Error)
    }

    return resp, nil
}

// CutChangeClothes 剪裁换正装
func (r *photoSpecApi) CutChangeClothes(req PhotoSpecCutChangeClothesRequest) (*PhotoSpecCutChangeClothesResponse, error) {
    // 参数格式化
    var err error
    params := make(map[string]interface{})
    jsonData, err := json.Marshal(req)
    if err != nil {
        return nil, err
    }
    err = json.Unmarshal(jsonData, &params)
    if err != nil {
        return nil, err
    }
    params["app_key"] = cutChangeClothesAppKey

    var resp = new(PhotoSpecCutChangeClothesResponse)
    uri := photoSpecUrl + "api/cut_change_clothes"
    dto := &RequestForm{
        Uri:    uri,
        Resp:   resp,
        Params: params,
    }
    err = Client.Post(dto)
    if err != nil {
        return nil, err
    }
    if resp.Error != "" {
        return nil, errors.New(resp.Error)
    }

    return resp, nil
}

// TakePicWm 获取带水印的图片地址
func (r *photoSpecApi) TakePicWm(photoName string) string {
    return photoSpecUrl + "api/take_pic_wm/" + photoName
}